<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改管理员信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="password"],
        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>修改管理员信息</h1>
        <form id="update-form">
            <label for="admin_name">用户名:</label>
            <input type="text" id="admin_name" name="admin_name" required>

            <label for="phone">手机号:</label>
            <input type="text" id="phone" name="phone" required>

            <label for="rights">权限:</label>
            <select id="rights" name="rights" required>
                <option value="normal">普通管理员</option>
                <option value="super">超级管理员</option>
            </select>

            <label for="password">新密码:</label>
            <input type="password" id="password" name="password" required>

            <button type="submit">提交修改</button>
        </form>
        <div id="message" class="message"></div>
    </div>

    <!-- 引入 axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>

        // 表单提交事件
        document.getElementById('update-form').addEventListener('submit', async function (event) {
            event.preventDefault(); // 阻止表单默认提交行为

            // 获取表单数据
            const formData = {
                admin_name: document.getElementById('admin_name').value,
                phone: document.getElementById('phone').value,
                rights: document.getElementById('rights').value,
                password: document.getElementById('password').value
            };

            try {
                // 发送 PUT 请求
                const adminId = getImageIdFromUrl();
                console.log('修改管理员信息:', adminId);
                const response = await axios.put(`http://127.0.0.1:5000/admin/admins/${adminId}`, formData);

                // 显示返回消息
                const messageDiv = document.getElementById('message');
                if (response.status === 200) {
                    messageDiv.className = 'message success';
                    messageDiv.textContent = response.data.message;
                } else {
                    messageDiv.className = 'message error';
                    messageDiv.textContent = response.data.message || '修改失败，请重试';
                }
            } catch (error) {
                console.error('请求失败:', error);
                const messageDiv = document.getElementById('message');
                messageDiv.className = 'message error';
                messageDiv.textContent = error.response?.data?.message || '网络错误，请重试';
            }
        });

        // 从URL中获取图片ID
        function getImageIdFromUrl() {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get('admin_id');
        }
    </script>
</body>
</html>